/*
 ============================================================================
 文件名 	: EasyBounceMainView.cpp
 描述		: EasyBounceMainView类的实现.
 ============================================================================
 */

//引用
#include <coemain.h>
#include <viewcli.h>
#include <aknviewappui.h>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>

#include <EasyBounce.rsg>

#include "EasyBounceMainView.h"
#include "EasyBounceMainContainer.h"
#include "EasyBounce.hrh"

_LIT( KTextEasy, "Easy" );
_LIT( KTextNormal, "Normal" );
_LIT( KTextHard, "Hard" );

const int KDefaultArraySegment = 4;

// ============================ 成员函数 ===============================

// ---------------------------------------------------------
// CFileFavorMainView::ConstructL()
// 两阶段构造函数.
// ---------------------------------------------------------
//
void CEasyBounceMainView::ConstructL()
    {
    BaseConstructL( R_EB_MAINVIEW );
    }

// ---------------------------------------------------------
// ---------------------------------------------------------
//
CEasyBounceMainView::CEasyBounceMainView()
	{
	//空实现
	}

// ---------------------------------------------------------
// CEasyBounceMainView::~CEasyBounceMainView()
// C++默认构造函数.
// ---------------------------------------------------------
//
CEasyBounceMainView::~CEasyBounceMainView()
    {
    if ( iContainer )
        {
        AppUi()->RemoveFromViewStack( *this, iContainer );
        }

    delete iContainer;
    iContainer=NULL;
    }

// ---------------------------------------------------------
// CEasyBounceMainView::Id()
// 返回视图ID
// ---------------------------------------------------------
//
TUid CEasyBounceMainView::Id() const
    {
    return KMainViewId;
    }

// ---------------------------------------------------------
// CEasyBounceMainView::HandleCommandL()
// 处理MainView对应的菜单命令 
// ---------------------------------------------------------
//
void CEasyBounceMainView::HandleCommandL(TInt aCommand)
    {
    switch ( aCommand )
        {
        default:
            {
            AppUi()->HandleCommandL( aCommand );
            break;
            }
        }
    }

// ---------------------------------------------------------
// CEasyBounceMainView::HandleClientRectChange()
// ---------------------------------------------------------
//
void CEasyBounceMainView::HandleClientRectChange()
    {
   /*
    if ( iContainer )
        {
        iContainer->SetRect( ClientRect() );
        }
    */
    }

// ---------------------------------------------------------
// CEasyBounceMainView::DoActivateL()
// 此函数在视图被激活时调用
// 视图所包含的container在此创建
// 视图被激活时可以接收消息，通过后两个参数来传递
// 参数aPrevViewId，上一个视图的ID
// 参数aCustomMessageId，消息ID
// 参数aCustomMessage，消息内容
// ---------------------------------------------------------
//
void CEasyBounceMainView::DoActivateL(
   const TVwsViewId& /*aPrevViewId*/,TUid /*aCustomMessageId*/,
   const TDesC8& /*aCustomMessage*/)
    {
    if (!iContainer)
        {
        iContainer = new (ELeave) CEasyBounceMainContainer;
        iContainer->SetMopParent(this);
        iContainer->ConstructL( ClientRect() );
        AppUi()->AddToStackL( *this, iContainer );
        }
   }

// ---------------------------------------------------------
// CEasyBounceMainView::DoDeactivate()
// 视图被反激活时调用此函数
// 此函数里进行对该视图拥有的container进行销毁
// ---------------------------------------------------------
//
void CEasyBounceMainView::DoDeactivate()
    {
    if ( iContainer )
    	{
    	AppUi()->RemoveFromViewStack( *this, iContainer );
    	}
   
    delete iContainer;
   	iContainer = NULL;
    }

// ---------------------------------------------------------
// CEasyBounceMainView::CmdNewGameL()
// 新建一个游戏
// ---------------------------------------------------------
//
void CEasyBounceMainView::CmdNewGameL()
	{
	TInt selectedLevel( 3 );

	CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat( KDefaultArraySegment );
	CleanupStack::PushL(array);

	array->AppendL( KTextEasy );
	array->AppendL( KTextNormal );
	array->AppendL( KTextHard );

	CAknListQueryDialog* dlg = new(ELeave) CAknListQueryDialog( &selectedLevel );
	dlg->PrepareLC( R_EB_LEVEL_DIALOG );
	dlg->SetItemTextArray(array);
	dlg->SetOwnershipType(ELbmDoesNotOwnItemArray);
	dlg->RunLD();
	
	if ( selectedLevel < 3 )
		{
		// 把用户选择的难度值传递给GameView
		TBuf8<10> level;
		level.Copy( array->MdcaPoint( selectedLevel ) );
		
   	    AppUi()->ActivateLocalViewL( 
   	       	    TUid::Uid( EGameViewId ),
   	       	    TUid::Uid( 10 ),
   	       	    level
   	       	    );
		}

	CleanupStack::PopAndDestroy(); //array
	}

// ---------------------------------------------------------
// CEasyBounceMainView::CmdShowRankL()
// 当Rank按钮被按下时，此函数被调用，以实现MainView和RankView的切换
// ---------------------------------------------------------
//
void CEasyBounceMainView::CmdShowRankL()
	{
	AppUi()->ActivateLocalViewL( TUid::Uid( ERankViewId ) );
	}


//文件结束

